package com.erp.module.jiehao

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.contacts.Contacts
import com.erp.util.Httputil.*
import org.xutils.http.HttpMethod
import org.xutils.http.RequestParams
import org.xutils.x

/**
 * Created by Administrator on 2019/3/19/019.
 * 作者：刘洋
 */
class JieHaoModel {
    /**分配节号*/
    fun setjiehao(context: Context, jo: String, jiehao: String, code: Int, onUserDataInterface: OnUserDataInterface) {
        val params: RequestParams
        if (code == 1) {
            params = RequestParams(Contacts.GET_JIEHAO_SHUJU + "/" + jiehao)
        } else if (code == 2) {
            params = RequestParams(Contacts.GET_ZHIXIANG_SHUJU + "/" + jiehao)
        } else {
            params = RequestParams(Contacts.GET_XISU_ZHIXIANG_SHUJU + "/" + jiehao)
        }
        getparams(context, params)
        addtoken(params)
        params.addHeader("Content-Type", "application/json")
        Log.d("TAAG", "jo=$jo")
        Log.d("TAAG", "p=$params")
        params.bodyContent = jo
        x.http().request(HttpMethod.PUT, params, Call(context, onUserDataInterface))
    }
    /**获取节号数据*/
    fun getJieHao(context: Context, page: Int, pagesize: Int, sousuo: String , code: Int, onUserDataInterface: OnUserDataInterface) {
        val params: RequestParams = if (code == 1) {
            RequestParams(Contacts.GET_JIEHAO_SHUJU)
        } else {
            RequestParams(Contacts.GET_ZHIXIANG_SHUJU)
        }
        params.addBodyParameter("page", page.toString())
        params.addBodyParameter("pagesize", pagesize.toString())
        params.addBodyParameter("searchParams[mb_batch]", sousuo)
        getparams(context, params)
        x.http().get<String>(params, Call(context, onUserDataInterface))
    }
    /**删除节号分配*/
    fun deletejiehao(context: Context, batch_names: String, onUserDataInterface: OnUserDataInterface){
        val params = RequestParams(Contacts.GET_JIEHAO_SHUJU + "/" + batch_names)
        getparams(context, params)
        addtoken(params)
        Log.d("deleteJieHao",params.toString());
        x.http().request(HttpMethod.DELETE, params, Call(context, onUserDataInterface))
    }

    /**上传节号*/
    fun addjiehao(context: Context, jo: String, code: Int, onUserDataInterface: OnUserDataInterface) {
        val params: RequestParams
        if (code == 1) {
            params = RequestParams(Contacts.GET_JIEHAO_SHUJU)
        } else {
            params = RequestParams(Contacts.GET_ZHIXIANG_SHUJU)
        }
        getparams(context, params)
        addtoken(params)
        params.addHeader("Content-Type", "application/json")
        params.bodyContent = jo
        x.http().post(params, Call(context, onUserDataInterface))
    }
}